package config

import (
	"fmt"
	"github.com/BurntSushi/toml"
)

type Config struct {
	Point *Point
}

type Point struct {
	ServerAddress string
	PointStatusUrl string
	PointDelayUrl string
	PointLoginUrl string
	StatusInterval int64
	LoginInterval int64
}

var GConfig *Config

func loadDefaultConfig(conf *Config) {
	if conf.Point == nil {
		conf.Point = &Point{
			"82.157.166.118:9191",
			"/saas/info/spa/v1/point/trigger",
			"/saas/info/spa/v1/point/delay",
			"/saas/info/spa/v1/point/login",
			6,
						20,
		}
		return
	}
	if conf.Point.PointStatusUrl == "" {
		conf.Point.PointStatusUrl = "/saas/info/spa/v1/point/trigger"
	} else if conf.Point.PointDelayUrl == "" {
		conf.Point.PointDelayUrl = "/saas/info/spa/v1/point/delay"
	} else if conf.Point.ServerAddress == "" {
		conf.Point.ServerAddress = "82.157.166.118:9191"
	} else if conf.Point.StatusInterval <= 0 {
		conf.Point.StatusInterval = 10
	}
}

func init(){
	//读取配置文件
	GConfig = &Config{}
	_, err := toml.DecodeFile("config/client.toml",GConfig)
	if err!=nil{
		fmt.Println(err)
	}
	// 加载默认配置
	loadDefaultConfig(GConfig)
}